<?php
class Backend_PostsController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	public function indexAction()
	{
		// helper
		$baseurl	= new MHDEV_View_Helper_BaseUrl();
		
		// vars
		$count		= 10; 
		$page		= intval($this->getRequest()->getParam('page', 0));
		$getPost	= $this->_request->getPost();
		
		// db
		$threads	= new Threads();
		$posts		= new Posts();
		$ranks		= new Rank();
		$bans		= new Bans();
		$smilies	= new Smilies();
		
		// query
		$getPosts	= $posts->fetchAll('is_public = 0 AND is_deleted = 0', 'create_date DESC', $count);
			
		// paginator
        $pagePosts	= Zend_Paginator::factory($getPosts->toArray());
        $pagePosts->setItemCountPerPage($count);
        $pagePosts->setCurrentPageNumber($page);
        
		// view
		$this->view->count			= $getPosts->count();
		$this->view->result			= $pagePosts;
	}
	
	public function requestAction()
	{
		// helper
		$pm			= new MHDEV_View_Helper_SendMessage();
		$translate	= Zend_Registry::get('lang');
		
		// vars
		$postData 	= $this->_request->getPost();
		
		$ids		= $postData['moderate'];
		$do			= $postData['do'];
		
		// db
		$posts		= new Posts();
		
		if($do == 'public')
		{
			foreach($ids AS $uid => $value)
			{
				$getPost 	= $posts->find($uid);
				$resPost	= $getPost->toArray();
				
				Zend_Registry::get('log')->info('[MODERATE] Post with ID ' . $uid . ' is now public. Author: ' . $resPost[0]['user_uid'] . ', Admin: ' . $this->auth->getIdentity()->uid);
				
				$posts->update(array('is_public' => 1), 'uid=' . $uid);
				$pm->SendMessage($resPost[0]['user_uid'], $translate->_('moderation_pm_post_public'), $translate->_('moderation_pm_post_public_text'));		
			}
		}
		elseif($do == 'delete')
		{
			foreach($ids AS $uid => $value)
			{
				$getPost 	= $posts->find($uid);
				$resPost	= $getPost->toArray();
				
				Zend_Registry::get('log')->info('[MODERATE] Post with ID ' . $uid . ' is now deleted. Author: ' . $resPost[0]['user_uid'] . ', Admin: ' . $this->auth->getIdentity()->uid);
				
				$pm->SendMessage($resPost[0]['user_uid'], $translate->_('moderation_pm_post_delete'), $translate->_('moderation_pm_post_delete_text'));
				$boards->update(array('count_posts' => new Zend_Db_Expr('count_posts-1')), 'uid=' . $resThread[0]['board_uid']);
				$threads->update(array('count_replies' => new Zend_Db_Expr('count_replies-1')), 'uid=' . $resThread[0]['uid']);
				$posts->delete('uid=' . $uid);
			}
		}
		
		$this->_helper->viewRenderer->setNoRender();
		$this->_redirect('/backend/posts.html');
	}
}
?>